/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelAutorizacao.java
 *
 * Created on 30/11/2011, 19:31:18
 */

package oogenda.view;

import java.util.Calendar;
import javax.swing.JOptionPane;
import oogenda.entidade.Agendamento;
import oogenda.entidade.Atendimento;
import oogenda.entidade.Paciente;
import oogenda.util.DadosObjetos;

/**
 *
 * @author Pablo
 */
public class PanelAutorizacao extends javax.swing.JPanel {

    private static PanelAutorizacao panel;

    public static PanelAutorizacao getPanel() {
        if(panel == null) {
            panel = new PanelAutorizacao();
        }

        panel.popularCombo();
        return panel;
    }

     /**
     * Preenche combobox Especialidades
     */
    public void popularCombo() {
        
        comboAgendamentos.removeAllItems();
        if(DadosObjetos.pacientes != null) {
            for(Paciente paciente : DadosObjetos.pacientes) {
                if(paciente.getAtendimento() != null) {
                    for(Atendimento atendimento : paciente.getAtendimento()) {
                        if(!atendimento.getM_Agendamento().isAutorizado()) {
                            comboAgendamentos.addItem(atendimento.getM_Agendamento());
                        }
                    }
                }
            }
        }
    }
    
    /** Creates new form PanelAutorizacao */
    public PanelAutorizacao() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        comboAgendamentos = new javax.swing.JComboBox();
        titulo = new javax.swing.JLabel();
        autorizar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jRadioButton6 = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();

        comboAgendamentos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        titulo.setFont(new java.awt.Font("Verdana", 1, 14));
        titulo.setText("Autorização");

        autorizar.setText("Autorizar");
        autorizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                autorizarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jLabel1.setText("Forma de Pagamento");

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Cortesia");

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("Cheque");

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText("Cartão");

        buttonGroup1.add(jRadioButton4);
        jRadioButton4.setText("Convenio");

        buttonGroup1.add(jRadioButton5);
        jRadioButton5.setText("Dinheiro");

        buttonGroup1.add(jRadioButton6);
        jRadioButton6.setText("Retorno");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton3))
                .addGap(65, 65, 65)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton6)
                    .addComponent(jRadioButton5)
                    .addComponent(jRadioButton4))
                .addContainerGap(156, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton3)
                    .addComponent(jRadioButton6))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jLabel2.setText("Agendamento:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(titulo, javax.swing.GroupLayout.DEFAULT_SIZE, 407, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(1, 1, 1)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                .addGap(335, 335, 335))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(131, Short.MAX_VALUE)
                .addComponent(autorizar, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(121, 121, 121))
            .addComponent(comboAgendamentos, 0, 407, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(titulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(5, 5, 5)
                .addComponent(comboAgendamentos, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(autorizar)
                .addGap(47, 47, 47))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void autorizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autorizarActionPerformed

        if(comboAgendamentos.getSelectedItem() == null) {
            JOptionPane.showMessageDialog(null, "Agendamento não informado.", "Validação", JOptionPane.ERROR_MESSAGE, null);
            return;
        }


        Agendamento agendamento = (Agendamento) comboAgendamentos.getSelectedItem();

       boolean flagAutorizar = false;


       if(jRadioButton1.isSelected()) {

           flagAutorizar = true;
           double valor = Math.random() * 100;
           int divisor = (int) valor % 5;
           System.out.println("valor: "+ valor);
           System.out.println("divisor: "+ divisor);
           if(divisor == 0) {
               flagAutorizar = false;
           }
            
       } else if (jRadioButton2.isSelected()) {

           flagAutorizar = true;
           double valor = Math.random() * 100;           
           int divisor = (int) valor % 2;
           System.out.println("valor: "+ valor);
           System.out.println("divisor: "+ divisor);
           if(divisor == 0) {
               flagAutorizar = false;
           }

       } else if(jRadioButton3.isSelected()) {

           flagAutorizar = true;

       } else if(jRadioButton4.isSelected()) {
            flagAutorizar = true;
           double valor = Math.random() * 100;
           int divisor = (int) valor % 10;
           System.out.println("valor: "+ valor);
           System.out.println("divisor: "+ divisor);
           if(divisor == 0) {
               flagAutorizar = false;
           }
       } else if(jRadioButton5.isSelected()) {
            flagAutorizar = true;
       } else if(jRadioButton6.isSelected()) {

           long _31dias = 1000 * 60 * 60 * 24 * 31;

           agendamento.getDataHora();
           Calendar dataAgendamento =  Calendar.getInstance();
           dataAgendamento.setTime(agendamento.getDataHora());

           Paciente paciente = agendamento.getPaciente();



           if(paciente.atendimento != null) {
               for(Atendimento atendimento : paciente.atendimento) {
                    if(atendimento.getM_Agendamento() != null) {
                        long diferenca = dataAgendamento.getTimeInMillis() - atendimento.getM_Agendamento().getDataHora().getTime();
                        
                        Calendar dataAgendamentoAnterior =  Calendar.getInstance();
                        dataAgendamento.setTime(agendamento.getDataHora());
                        dataAgendamento.add(Calendar.DAY_OF_MONTH, -31);

                        boolean diferenca2 = dataAgendamento.getTimeInMillis() < dataAgendamentoAnterior.getTimeInMillis();
                        
                        if( diferenca > 0 && diferenca2) {
                           flagAutorizar = true;
                        }
                    }
               }
           }

           if(flagAutorizar == false) {
               JOptionPane.showMessageDialog(null, "Não é possível autorizar o retorno, pois não existe um atendimento autorizado com data inferior a 31 dias para este paciente.", "Validação", JOptionPane.ERROR_MESSAGE, null);
               return;
           }

       } else {
           JOptionPane.showMessageDialog(null, "Indique a forma de pagamento", "Validação", JOptionPane.ERROR_MESSAGE, null);
           return;
       }


       if(flagAutorizar) {
           JOptionPane.showMessageDialog(null, "Autorizado", "Validação", JOptionPane.INFORMATION_MESSAGE, null);
           agendamento.setAutorizado(true);
       } else {
           JOptionPane.showMessageDialog(null, "Não Autorizado", "Validação", JOptionPane.ERROR_MESSAGE, null);
           agendamento.setAutorizado(false);
       }

       popularCombo();


    }//GEN-LAST:event_autorizarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton autorizar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JComboBox comboAgendamentos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    private javax.swing.JRadioButton jRadioButton6;
    private javax.swing.JLabel titulo;
    // End of variables declaration//GEN-END:variables

}
